/*
 * Copyright 2010 Guillaume Amringer
 * 
 * This file is part of WLoader.
 * 
 * WLoader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * WLoader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with WLoader.  If not, see <http://www.gnu.org/licenses/>.
 * */
package ga.wloader.net {
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import ga.wloader.events.WNetEvent;
	
	public class WURLRequest extends URLLoader {
		
		public static const BITMAP:String = "bitmap";
		public static const TEXT:String = "text";
		public static const XML_DATA:String = "xml_data";
		
		public var url:URLRequest;
		public var id:String;
		public var metaAvailable:Boolean = false;
		public var requested:Boolean = false;
		public var type:String = WURLRequest.TEXT;
		
		private var bitmapLoader:Loader;
		public var bitmap:Bitmap;
		
		public function WURLRequest(pOptions:Object) {
			url = new URLRequest(pOptions.url);
			id = pOptions.id;
			
			if (pOptions.type is String) {
				type = pOptions.type;
			} else if (pOptions.url.match(/.png|.gif|.jpg|.jpeg$/)) {
				type = WURLRequest.BITMAP;
			} else if (pOptions.url.match(/.xml$/)) {
				type = WURLRequest.XML_DATA;
			}
			
			if (this.type == WURLRequest.BITMAP) {
				this.dataFormat = URLLoaderDataFormat.BINARY;
			} else {
				this.dataFormat = URLLoaderDataFormat.TEXT;
			}
			
			this.addEventListener(ProgressEvent.PROGRESS, _progressHandler);
			this.load(url);
		}
		
		public override function toString():String {
			return 'WURLRequest(' + id + '): ' + url.url + ' | Meta: ' + metaAvailable + ' | Type: ' + type;
		}
		
		public function start():void {
			
			this.addEventListener(Event.COMPLETE, _completeHandler);
			
			this.requested = true;
			if (this.metaAvailable) {
				this.load(url);
			}
		}
		
		private function _progressHandler(e:ProgressEvent):void {
			this.removeEventListener(ProgressEvent.PROGRESS, _progressHandler);
			this.metaAvailable = true;
			this.dispatchEvent(new WNetEvent(WNetEvent.META_DATA));
			if (!this.requested) {
				this.close();
			}
		}
		
		private function _completeHandler(e:Event):void {
			if (this.type == WURLRequest.BITMAP) {
				bitmapLoader = new Loader();
				bitmapLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, _bitmapLoaderCompleteHandler);
				bitmapLoader.loadBytes(this.data);
				
			//	TODO -> Support for XML
				
			} else {
				this.dispatchEvent(new WNetEvent(WNetEvent.READY));
			}
		}
		
		private function _bitmapLoaderCompleteHandler(e:Event):void {
			this.bitmap = e.target.content;
			bitmapLoader = null;
			e.target.removeEventListener(Event.COMPLETE, _bitmapLoaderCompleteHandler);
			
			this.dispatchEvent(new WNetEvent(WNetEvent.READY));
		}
	}
}
